﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Edge.NetApi;
using Microsoft.Win32;
using SOA.Persistent;
using System.Configuration;

namespace LG_CLIENT_HEART
{
    class Program
    {
        private static Socket socketSend;
        private static Socket SocketSend_Heart;
        private static MemDb db = new MemDb();
        private static bool Recv_Flag = false;
        private static Thread s_thread;//客户心跳线程实例
        private static string _pass = ConfigurationManager.AppSettings["pass"];
        static void Main(string[] args)
        {
            try
            {
                //创建客户端Socket，获得远程ip和端口号
                Connect();
                ////开启新的线程，不停的接收服务器发来的消息
                //Thread c_thread = new Thread(Received);
                //c_thread.IsBackground = true;
                //c_thread.Start();

                ///客户端发送
                s_thread = new Thread(SendObj);
                s_thread.IsBackground = true;
                s_thread.Start();

                //L2设定值回应
                Thread S_DataThread = new Thread(SendData);
                S_DataThread.IsBackground = true;
                S_DataThread.Start();

                ///监测Recv_Flag判断是否中断心跳
                Thread Monthread = new Thread(RestartSendObj);
                Monthread.IsBackground = true;
                Monthread.Start();

                SetStartWithStartUp();//设置开自动重启

                Console.ReadLine();
            }
            catch (Exception ex)
            {
                db.Write("FORWARD_RECV.L2_SEND_Sign", 1);
                Console.WriteLine("连接失败:" + ex.Message);
                Logger<string>.Debug(ex.Message);
            }


        }

        #region 注册自动重启
        //程序的名称
        private const string SoftWareName = "LG_CLIENT_HEART";

        private static void SetStartWithStartUp()
        {
            string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            // 针对当前用户添加注册表启动项
            RegistryKey rk = Registry.CurrentUser;
            try
            {
                //注册表位置   
                RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");

                // 检测是否之前有设置自启动了，如果设置了，就看值是否一样
                string old_path = (string)rk2.GetValue(SoftWareName);

                if (!path.Equals(old_path))
                {
                    //写入值
                    rk2.SetValue(SoftWareName, path);
                    Console.WriteLine("设置开机自启成功");
                }
                else
                {
                    Console.WriteLine("已设置过开机自启");
                }

                rk2.Close();
                rk.Close();

            }
            catch (Exception e)
            {
                Console.WriteLine($"开机自启动设置失败:{e}");
            }
        }

        #endregion


        private static void Connect()
        {
            try
            {
                socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socketSend.ReceiveTimeout = 30000;
                IPAddress ip = IPAddress.Parse("10.9.62.40");
                IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32("10102"));
                socketSend.Connect(point);

                //SocketSend_Heart = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //SocketSend_Heart.Connect(point);


                Console.WriteLine("连接成功!");
            }
            catch (Exception ex)
            {
                return;
            }

        }

        /// <summary>
        /// 接收线程
        /// </summary>
        private static void Received()
        {
            Console.WriteLine("开始接收。");
            while (true)
            {

                try
                {
                    Recv_Flag = false;
                    if (!socketSend.Connected) continue;
                    byte[] buffer = new byte[49];
                    //实际接收到的有效字节数
                    int len = socketSend.Receive(buffer);
                    Recv_Flag = true;
                    if (len == 0)
                    {
                        Console.WriteLine("接收心跳字节为0");
                        // break;
                        continue;
                    }
                    string str = Encoding.UTF8.GetString(buffer, 0, len);

                    string c_MsgID = str.Substring(0, 8);
                    if (c_MsgID == "20220003")
                    {
                        Logger<string>.Info("打印接收心跳字节------" + str);
                        Console.WriteLine("打印接收心跳字节------:" + str);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Received方法出现异常" + ex.Message);
                    Logger<string>.Debug(ex.Message);
                }
            }
        }

        private static bool Send(string msg)
        {
            bool flag = false;
            try
            {
                //byte[] buffer = new byte[1024 * 1024 * 3];
                byte[] buffer = Encoding.UTF8.GetBytes(msg);
                socketSend.Send(buffer);
                flag = true;
                db.Write("FORWARD_RECV.L2_SEND_Sign", 0);
            }
            catch (Exception ex)
            {
                db.Write("FORWARD_RECV.L2_SEND_Sign", 1);
                Console.WriteLine("Send方法出现异常" + ex.Message);
                socketSend.Dispose();
                socketSend = null;
                Connect();

                Logger<string>.Debug(ex.Message);

            }
            return flag;
        }
        static string Previous_Slab = string.Empty;//上一块板坯号
        private static void SendData()
        {
            int count = Convert.ToInt32(db.Read("FORWARD_RECV.TCP_Counter"));
            int Send_Counter = Convert.ToInt32(db.Read("RR_R1_L2_SEND.T_R1_L2_Telegram_Counter"));
            int Cur_count = 1;
            while (true)
            {
                Thread.Sleep(100);
                int cur_count = Convert.ToInt32(db.Read("FORWARD_RECV.TCP_Counter"));
                if (cur_count != count)
                {
                    int send_counter = Convert.ToInt32(db.Read("RR_R1_L2_SEND.T_R1_L2_Telegram_Counter"));
                    #region L2回应设定值 先使用测试数据 后续可调用接口传值实现下发
                    if (Send_Counter != send_counter)
                    {
                        string c_SendDate = DateTime.Now.Date.ToString("yyyyMMdd");
                        int id = Convert.ToInt32(db.Read("FORWARD_RECV.ModelCheckID"));
                        string msg = string.Empty;//总信息

                        TimeSpan mTimeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
                        //得到精确到秒的时间戳（长度10位）
                        long time = (long)mTimeSpan.TotalSeconds;

                        string S_c_MsgID = "20230002";
                        string S_c_SendDate = DateTime.Now.Date.ToString("yyyyMMdd");
                        string S_c_SendTimeStamp = time.ToString().Substring(0, 6);
                        string S_c_Sender = "RMRX";
                        string S_c_Reciever = "RXRM";
                        string S_c_MsgLen = "000148";
                        string S_c_SequenceNo = Cur_count.ToString();//send_counter.ToString();
                        string S_c_SlabNo = db.Read("RR_R1_L2_RECV_SINGLE.F_R1_L2_Slab_ID").ToString();


                        string S_c_Pass = db.Read("RR_R1_L2_RECV_SINGLE.F_R1_L2_Current_Pass_No").ToString(); //当前道次    //"1";
                        _pass = ConfigurationManager.AppSettings["pass"];
                        if (S_c_Pass == _pass)
                        {
                            goto DirecBreak;
                        }
                        if (S_c_Pass == "1")
                        {
                            Previous_Slab = db.Read("SEND_READY.Slab_ID").ToString();//获取上一块板坯号
                        }

                        int pass = Convert.ToInt32(S_c_Pass);
                        //switch (id)
                        //{
                        //    case 0:
                        //        if (pass % 2!=0)//机前单控 奇数道次 1 3 5 7 奇数道次不控制 跳走 道次号传0
                        //        {
                        //            msg = S_c_MsgID + S_c_SendDate + S_c_SendTimeStamp + S_c_Sender + S_c_Reciever + S_c_SequenceNo + S_c_SlabNo + "0";
                        //            msg = msg.PadRight(148, '0');
                        //            goto DirecBreak;
                        //        }
                        //        break;
                        //    case 1:
                        //        if (pass % 2==0)//机后单控 偶数道次不控制 2 4 6 跳走 道次号传0
                        //        {
                        //            msg = S_c_MsgID + S_c_SendDate + S_c_SendTimeStamp + S_c_Sender + S_c_Reciever + S_c_SequenceNo + S_c_SlabNo + "0";
                        //            msg=  msg.PadRight(148, '0');
                        //            goto DirecBreak;
                        //        }
                        //        break;
                        //}


                        double T_R1_L2_TiltWhole = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_TiltWhole")) * 100, 0);//1代表正数 0代表负数 正常情况下双控
                        if (id == 1)//单控
                        {
                            if (pass % 2 == 0) //单控偶数道次下发上一块调平值
                            {
                                string pre_Res = Previous_Search(Previous_Slab, pass);
                                if (pre_Res != "fail")
                                {
                                    T_R1_L2_TiltWhole = Math.Round(Convert.ToDouble(pre_Res) * 100, 0);
                                }
                                else
                                {
                                    T_R1_L2_TiltWhole = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_TiltWhole")) * 100, 0);//如果没找到 就发本道次调平值
                                }
                            }
                        }

                        string S_c_GapLevel = T_R1_L2_TiltWhole > 0 ? "1" + T_R1_L2_TiltWhole : "0" + Math.Abs(T_R1_L2_TiltWhole);


                        double T_R1_L2_Proportion_Hd = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Hd")) * 100, 0);//头 中 尾比例
                        string S_c_HeadPoint = T_R1_L2_Proportion_Hd > 0 ? "1" + T_R1_L2_Proportion_Hd : "0" + Math.Abs(T_R1_L2_Proportion_Hd);

                        double T_R1_L2_Proportion_Mid = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Mid")) * 100, 0);
                        string S_c_BodyPoint = T_R1_L2_Proportion_Mid > 0 ? "1" + T_R1_L2_Proportion_Mid : "0" + Math.Abs(T_R1_L2_Proportion_Mid);

                        double T_R1_L2_Proportion_Tl = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Tl")) * 100, 0);
                        string S_c_TailPoint = T_R1_L2_Proportion_Tl > 0 ? "1" + T_R1_L2_Proportion_Tl : "0" + Math.Abs(T_R1_L2_Proportion_Tl);

                        double T_R1_L2_Camber_Hd = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Hd")), 0);//头 中 尾部偏移量 只看头
                        string S_c_LastHeadDev = T_R1_L2_Camber_Hd > 0 ? "1" + T_R1_L2_Camber_Hd : "0" + Math.Abs(T_R1_L2_Camber_Hd);

                        double T_R1_L2_Camber_Mid = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Mid")), 0);
                        string S_c_LastBodyDev = T_R1_L2_Camber_Mid > 0 ? "1" + T_R1_L2_Camber_Mid : "0" + Math.Abs(T_R1_L2_Camber_Mid);

                        double T_R1_L2_Camber_Tl = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Tl")), 0);
                        string S_c_LastTailDev = T_R1_L2_Camber_Tl > 0 ? "1" + T_R1_L2_Camber_Tl : "0" + Math.Abs(T_R1_L2_Camber_Tl);

                        double T_R1_L2_TiltFF_Hd = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Hd")) * 100, 0);//头 中 尾设定值 无用
                        string S_c_HeadGapLevel = T_R1_L2_TiltFF_Hd > 0 ? "1" + T_R1_L2_TiltFF_Hd : "0" + Math.Abs(T_R1_L2_TiltFF_Hd);

                        double T_R1_L2_TiltFF_Mid = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Mid")) * 100, 0);
                        string S_c_BodyGapLevel = T_R1_L2_TiltFF_Mid > 0 ? "1" + T_R1_L2_TiltFF_Mid : "0" + Math.Abs(T_R1_L2_TiltFF_Mid);

                        double T_R1_L2_TiltFF_Tl = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Tl")) * 100, 0);
                        string S_c_TailGapLevel = T_R1_L2_TiltFF_Tl > 0 ? "1" + T_R1_L2_TiltFF_Tl : "0" + Math.Abs(T_R1_L2_TiltFF_Tl);


                        // string S_c_GapLevel = db.Read("RR_R1_L2_SEND.T_R1_L2_TiltWhole").ToString();
                        //string S_c_HeadPoint = db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Hd").ToString();
                        //string S_c_BodyPoint = db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Mid").ToString();
                        //string S_c_TailPoint = db.Read("RR_R1_L2_SEND.T_R1_L2_Proportion_Tl").ToString();
                        //string S_c_LastHeadDev = db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Hd").ToString();
                        //string S_c_LastBodyDev = db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Mid").ToString();
                        //string S_c_LastTailDev = db.Read("RR_R1_L2_SEND.T_R1_L2_Camber_Tl").ToString();
                        //string S_c_HeadGapLevel = db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Hd").ToString();
                        //string S_c_BodyGapLevel = db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Mid").ToString();
                        //string S_c_TailGapLevel = db.Read("RR_R1_L2_SEND.T_R1_L2_TiltFF_Tl").ToString();

                        double E1_Offset = Math.Round(Convert.ToDouble(db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_1")) * 10, 0);//立辊偏移
                        //string S_c_Backup1 = E1_Offset > 0 ? "1" + E1_Offset : "0" + Math.Abs(E1_Offset);
                        string S_c_Backup1 = string.Empty;
                        if (Math.Abs(E1_Offset) < 100)
                        {
                            S_c_Backup1 = E1_Offset > 0 ? "10" + E1_Offset : "00" + Math.Abs(E1_Offset);
                        }
                        else
                        {
                            S_c_Backup1 = E1_Offset > 0 ? "1" + E1_Offset : "0" + Math.Abs(E1_Offset);
                        }

                        string S_c_Backup2 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_2").ToString();
                        //  string S_c_Backup2 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_2").ToString();

                        string S_c_Backup3 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_3").ToString();
                        string S_c_Backup4 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_4").ToString();
                        string S_c_Backup5 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_5").ToString();
                        string S_c_Backup6 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_6").ToString();
                        string S_c_Backup7 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_7").ToString();
                        string S_c_Backup8 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_8").ToString();
                        string S_c_Backup9 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_9").ToString();
                        string S_c_Backup10 = db.Read("RR_R1_L2_SEND.T_R1_L2_Reserve_Float_10").ToString();

                        msg = S_c_MsgID + S_c_SendDate + S_c_SendTimeStamp + S_c_Sender + S_c_Reciever + S_c_MsgLen.PadLeft(6, '0') + S_c_SequenceNo.PadLeft(4, '0') + S_c_SlabNo.PadRight(24, ' ')
                        + S_c_Pass.PadRight(4, ' ') + S_c_GapLevel.PadRight(4, ' ') + S_c_HeadPoint.PadRight(4, ' ') + S_c_BodyPoint.PadRight(4, ' ') + S_c_TailPoint.PadRight(4, ' ') + S_c_LastHeadDev.PadRight(4, ' ')
                        + S_c_LastBodyDev.PadRight(4, ' ') + S_c_LastTailDev.PadRight(4, ' ') + S_c_HeadGapLevel.PadRight(4, ' ') + S_c_BodyGapLevel.PadRight(4, ' ') + S_c_TailGapLevel.PadRight(4, ' ')
                        + S_c_Backup1.PadRight(4, '0') + S_c_Backup2.PadRight(4, '0') + S_c_Backup3.PadRight(4, '0') + S_c_Backup4.PadRight(4, '0') + S_c_Backup5.PadRight(4, '0') + S_c_Backup6.PadRight(4, '0')
                        + S_c_Backup7.PadRight(4, '0') + S_c_Backup8.PadRight(4, '0') + S_c_Backup9.PadRight(4, '0') + S_c_Backup10.PadRight(3, '0') + "\n";



                        if (!string.IsNullOrEmpty(msg))
                        {
                            bool flag = Send(msg);
                            if (flag)
                            {
                                Console.WriteLine("下发测试设定值发送完成：" + msg + "*****字节长度*****" + msg.Length);
                                Logger<string>.Info("下发测试设定值发送完成：" + msg);
                                Console.WriteLine("下发测试设定值发送时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            }
                            else
                            {
                                Console.WriteLine("下发测试设定值发送失败：" + msg + "*****字节长度*****" + msg.Length);
                                Logger<string>.Info("下发测试设定值发送失败：" + msg);
                            }

                        }
                        Cur_count++;

                        if (Cur_count >= 10000)
                        {
                            Cur_count = 1;
                        }

                        count = cur_count;
                        Send_Counter = send_counter;

                    DirecBreak:;
                        #endregion
                    }
                }
            }
        }


        private static string strConn = "server=localhost;database=lg2250;SslMode=None;user id=root;password=123456;port=3306;charset=utf8";
        private static string Previous_Search(string slab, int pass)
        {

            DataTable dt = new DataTable();
            // 连接Mysql数据库
            using (IDbContext sdb = new MySqlDataStore(strConn))
            {
                try
                {
                    // 1.将收集数据写入数据库。
                    StringBuilder strSql = new StringBuilder();
                    // 获取表名
                    string strTableName = "r1_send_ready_" + DateTime.Now.ToString("yyyy");
                    // 开启事务
                    sdb.BeginTransaction();
                    strSql.Append(" Select Gap_leveling_Control_Value from " + strTableName);
                    strSql.Append(" where Current_PassNo= " + pass);
                    strSql.Append(" and Slab_ID='" + slab + "'");
                    dt = sdb.Query(strSql.ToString());
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        return dt.Rows[0][0].ToString();
                    }
                    else
                    {
                        return "fail";
                    }

                }
                catch (Exception ex)
                {
                    Logger<string>.Info($"{System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName} -数据库查询异常；异常信息：{ex.ToString()}");
                    return "fail";
                }
            }

        }


        private static int index = 0;//异常信号索引 0为正常，1为异常
        private static int count = 0;//计数3次 重启三次后抛出异常 
        public static void RestartSendObj()
        {
            while (true)
            {
                Thread.Sleep(500);
                if (count < 3)
                {
                    if (index == 1)
                    {
                        if (!s_thread.IsAlive)
                        {
                            s_thread = null;
                            s_thread = new Thread(SendObj);
                            s_thread.IsBackground = true;
                            s_thread.Start();
                            index = 0;
                            count++;
                        }
                    }
                }


            }

        }


        private static void SendObj()
        {
            try
            {
                int c_SequenceNo = 0;
                while (true)
                {
                    try
                    {
                        Thread.Sleep(30000);
                        string c_MsgID = "20230001";
                        string c_SendDate = DateTime.Now.Date.ToString("yyyyMMdd");
                        TimeSpan mTimeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
                        //得到精确到秒的时间戳（长度10位）
                        long time = (long)mTimeSpan.TotalSeconds;
                        string c_SendTimeStamp = time.ToString().Substring(0, 6);
                        string c_Sender = "RMRX";
                        string c_Reciever = "RXRM";
                        string c_MsgLen = "000044";
                        string c_Health = "001";
                        string cetx = "\n";
                        db.Write("L2_SEND_HEART_MODEL.F_R1_L2_Telegram_ID", c_MsgID);
                        db.Write("L2_SEND_HEART_MODEL.c_SendDate", c_SendDate);
                        db.Write("L2_SEND_HEART_MODEL.c_SendTimeStamp", c_SendTimeStamp);
                        db.Write("L2_SEND_HEART_MODEL.c_Sender", c_Sender);
                        db.Write("L2_SEND_HEART_MODEL.c_Reciever", c_Reciever);
                        db.Write("L2_SEND_HEART_MODEL.F_R1_L2_Telegram_Length", c_MsgLen);
                        db.Write("L2_SEND_HEART_MODEL.F_R1_L2_Telegram_Counter", c_SequenceNo);
                        db.Write("L2_SEND_HEART_MODEL.c_ETX", cetx);
                        db.Write("L2_SEND_HEART_MODEL.c_Health", c_Health);

                        c_SequenceNo++;
                        if (c_SequenceNo >= 1000)
                        {
                            c_SequenceNo = 1;
                        }
                        string msg = c_MsgID + c_SendDate + c_SendTimeStamp + c_Sender + c_Reciever + c_MsgLen + c_SequenceNo.ToString().PadLeft(4, '0') + c_Health.PadLeft(3, '0') + cetx;

                        bool flag = Send(msg);
                        if (flag)
                        {
                            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm;ss:fff") + "  心跳发送完成:" + msg);
                            Logger<string>.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm;ss:fff") + "  心跳发送完成：" + msg);

                            if (!socketSend.Connected) continue;
                            byte[] buffer = new byte[49];
                            //实际接收到的有效字节数
                            int len = socketSend.Receive(buffer);
                            if (len == 0)
                            {
                                Console.WriteLine("接收心跳字节为0");
                                Exception ex = new Exception("接收心跳字节为0");
                                Logger<string>.Debug("接收心跳字节为0");
                                throw ex;
                            }
                            string str = Encoding.UTF8.GetString(buffer, 0, len);

                            string c_MsgID_Recv = str.Substring(0, 8);
                            if (c_MsgID_Recv == "20220003")
                            {
                                Logger<string>.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm;ss:fff") + "  打印接收心跳字节------" + str);
                                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm;ss:fff") + "  打印接收心跳字节------:" + str);
                                count = 0;
                            }

                        }
                        else
                        {
                            Console.WriteLine("心跳发送失败:" + msg);
                            Logger<string>.Info("心跳发送失败：" + msg);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("SendObj方法出现异常" + ex.Message);
                        Logger<string>.Debug("SendObj方法出现异常" + ex.Message);
                    }


                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("SendObj方法出现异常" + ex.Message);
                Logger<string>.Debug("SendObj方法出现异常" + ex.Message);

                if (count < 3)
                {
                    index = 1;
                    socketSend.Dispose();
                    socketSend = null;
                    Connect();
                    if (s_thread != null)
                    {
                        s_thread.Abort();
                        s_thread = null;
                    }
                }
                else
                {
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "多次重启连接，发送失败！");
                    Logger<string>.Debug(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "多次重启连接，发送失败！");
                }

            }




        }
    }
}
